//
//  InfoViewController.m
//  project-A (pianke)
//
//  Created by dllo on 16/1/31.
//  Copyright © 2016年 scott. All rights reserved.
//

#import "InfoViewController.h"
#import "CellOfLike.h"
#import "CellofTrips.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import "VCtravles.h"
#import "ModelOfTravl.h"
#import "LikeOfVC.h"
#import "ModelOfLike.h"
#import <DKNightVersion.h>

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface InfoViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,traveldelegate,Likedelegate>
@property (nonatomic,retain)UICollectionView *collect;



@property (nonatomic,retain)UIView *backview;

@property (nonatomic,retain)UIImageView *imageview;
@property (nonatomic,retain)UILabel *tripCount;

@property (nonatomic,retain)UILabel *name;

@property (nonatomic,retain)UIButton *guanzhu;

@property (nonatomic,retain)UIButton *sixin;

@property (nonatomic,retain)UIButton *youji;

@property (nonatomic,retain)UIButton *xihuan;

@property (nonatomic,retain)UIView *viewOfred;

@property (nonatomic,retain)NSDictionary *dic;

@property (nonatomic,retain)NSString *strname;

@property (nonatomic,retain)NSString *strimage;

@property (nonatomic,retain)NSString *strcount;

@property (nonatomic,retain)NSString *strsex;

@end

@implementation InfoViewController

-(void)dealloc{
    
    [_idstring2 release];
    [_strcount release];
    [_strimage release];
    [_strname release];
    [_idstring release];
    [_viewOfred release];
    [_collect release];
    [_imageview release];
    [_tripCount release];
    [_backview release];
    [_name release];
    [_guanzhu release];
    [_sixin release];
    [_youji release];
    [_xihuan release];
    [_dic release];
    [_strsex release];
    [super dealloc];
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.backview = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.backview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.backview];
    
    [self.backview release];
    
   self.name.dk_textColorPicker  = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:0.16 green:0.14 blue:0.13 alpha:1]);
    
    self.tripCount.dk_textColorPicker  = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:0.16 green:0.14 blue:0.13 alpha:1]);
    
    [self anaylsesData];
    
    [self creatInfo];
    
    [self creatcollectionview];
    
    [self creatKVOAction];
    
    
}
#pragma mark 解析数据

-(void)anaylsesData{
    
   
    
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    
    NSURL *URL = [NSURL URLWithString:self.idstring];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"阿萨大大的");
        } else {
            
            
           
            self.strimage = [NSString stringWithString:[responseObject objectForKey:@"image"]];
            self.strname = [NSString stringWithString:[responseObject objectForKey:@"name"]];

            self.strcount = [NSString stringWithFormat:@"%@篇游记",[responseObject objectForKey:@"trips_count"]];
            
            self.strsex = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"gender"]];
            
            
            self.imageview.backgroundColor =[UIColor redColor];
            [self.imageview sd_setImageWithURL:[NSURL URLWithString:self.strimage] placeholderImage:nil options:SDWebImageAvoidAutoSetImage];
            
            self.name.text = self.strname;
            
            self.tripCount.text = self.strcount;
            
            if ([self.strsex isEqualToString:@"0"]) {
                [self.guanzhu setTitle:@"关注她" forState:UIControlStateNormal];
                [self.guanzhu setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                 [self.sixin setTitle:@"私信她" forState:UIControlStateNormal];
            }else{
                [self.sixin setTitle:@"私信他" forState:UIControlStateNormal];
               [self.guanzhu setTitle:@"关注他" forState:UIControlStateNormal];
                [self.guanzhu setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            }
            
        }
    }];
    [dataTask resume];
    
}



-(void)creatInfo{
    CGFloat width = self.backview.frame.size.width;
    
    self.imageview = [[UIImageView alloc]initWithFrame:CGRectMake(10 , 74, 40, 40)];
    [self.view addSubview:self.imageview];
   
    
    [self.imageview release];
    
    self.name = [[UILabel alloc]initWithFrame:CGRectMake(65, 74, 0.3 * width, 25)];
    [self.name setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];;
   
    
    
    [self.backview addSubview:self.name];
    [self.name release];
    
    self.tripCount = [[UILabel alloc]initWithFrame:CGRectMake(65, 104, 0.3 *width, 14)];
    
    self.tripCount.font = [UIFont systemFontOfSize:12];
    [self.backview addSubview:self.tripCount];
    
    
    
    [self.tripCount release];
    
    
    
    self.guanzhu = [UIButton buttonWithType:UIButtonTypeCustom];
    self.guanzhu.frame = CGRectMake(10, 129, (width - 30)/2, 30);
    [self.guanzhu setTitle:@"关注" forState:UIControlStateNormal];
    self.guanzhu.backgroundColor = [UIColor lightGrayColor];
    [self.guanzhu setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.guanzhu  addTarget:self action:@selector(guanzhu:) forControlEvents:UIControlEventTouchUpInside];
    [self.backview addSubview:self.guanzhu];
    
    
    self.sixin = [UIButton buttonWithType:UIButtonTypeCustom];
    self.sixin.frame = CGRectMake(20 + (width - 30)/2, 129, (width - 30)/2, 30);
    self.sixin.backgroundColor = [UIColor lightGrayColor];
    [self.sixin setTitle:@"私信" forState:UIControlStateNormal];
    [self.sixin setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.sixin addTarget:self action:@selector(sixin:) forControlEvents:UIControlEventTouchUpInside];
    [self.backview addSubview:self.sixin];
       
    
    self.youji = [[UIButton alloc]initWithFrame:CGRectMake(0, 169, width / 2, 40)];
    
    self.youji.backgroundColor = [UIColor orangeColor];
    [self.youji setTitle:@"游记" forState:UIControlStateNormal];
    [self.youji addTarget:self action:@selector(youji:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.backview addSubview:self.youji];
    [self.youji release];
    
    self.xihuan = [[UIButton alloc]initWithFrame:CGRectMake(width / 2, 169, width / 2, 40)];
    
    self.xihuan.backgroundColor = [UIColor orangeColor];
    [self.xihuan setTitle:@"喜欢" forState:UIControlStateNormal];
    [self.xihuan addTarget:self action:@selector(xihuan:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.backview addSubview:self.xihuan];
    [self.xihuan release];

    
    
    self.viewOfred = [[UIView alloc]initWithFrame:CGRectMake(0,209, width / 2, 3)];
    self.viewOfred.backgroundColor = [UIColor blueColor];
    
    [self.backview addSubview:self.viewOfred];
    [self.viewOfred release];
    
}

#pragma mark 按键动作

-(void)xihuan:(UIButton *)button{
    
    [self.collect setContentOffset:CGPointMake(WIDTH, 0) animated:YES];
}


-(void)youji:(UIButton *)buuton{
    [self.collect setContentOffset:CGPointMake(0, 0) animated:YES];

}

-(void)guanzhu:(UIButton *)button{
    
    NSLog(@"关注成功");
    
}

-(void)sixin:(UIButton *)button{
    
    NSLog(@"私信成功");
    
}

-(void)creatcollectionview{
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
    flowlayout.itemSize = CGSizeMake(WIDTH, HEIGHT - 212);
    flowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowlayout.minimumInteritemSpacing = 0;
    flowlayout.minimumLineSpacing = 0;
    
    
    self.collect = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 212, WIDTH, HEIGHT - 212) collectionViewLayout:flowlayout];
    [self.backview addSubview:self.collect];
    self.collect.dataSource = self;
    self.collect.delegate = self;
    
    self.collect.backgroundColor = [UIColor lightGrayColor];
    self.collect.pagingEnabled = YES;
    self.collect.bounces = NO;
    
    [self.collect registerClass:[CellofTrips class] forCellWithReuseIdentifier:@"poolOfTrips"];
    
    [self.collect registerClass:[CellOfLike class] forCellWithReuseIdentifier:@"poolOfLike"];
    [flowlayout release];
    [self.collect release];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return 2;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        CellofTrips *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolOfTrips" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor cyanColor];
        
        cell.delegate = self;
       [cell geturlwith:self.idstring];
        return cell;
    }else{
        CellOfLike *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolOfLike" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor whiteColor];
        
        
        cell.delegate = self;
        [cell geturlwith:self.idstring2];
        return cell;
    
    }
}

-(BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
}

-(void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(nonnull NSIndexPath *)sourceIndexPath toIndexPath:(nonnull NSIndexPath *)destinationIndexPath{
    
    
}

#pragma mark  协议方法

-(void)didClick:(ModelOfTravl *)model{
    
    NSLog(@"旅游行程界面个人");
    VCtravles *travel = [[VCtravles alloc]init];
    NSString *strid = [NSString stringWithFormat:@"%@",model.idd];
    NSString *str = [NSString stringWithFormat:@"http://chanyouji.com/api/trips/%@.json",strid];
    NSLog(@"%@",str);
    travel.strURl = str;
    [self.navigationController pushViewController:travel animated:YES];
    //    [travel release];

    
}

-(void)didlikeClick:(ModelOfLike *)model{
    
    NSLog(@"喜欢的图界面");
    LikeOfVC *likevc = [[LikeOfVC alloc]init];
    
    likevc.photo_url = model.photo_url;
    
    likevc.descript = model.description1;
    
    likevc.width = model.width.integerValue;
    
    likevc.height = model.height.integerValue;
    
    likevc.strtrip = model.trip_id;
    
    NSLog(@"%ld",likevc.width);
    
    NSLog(@"%ld",likevc.height);
    
    NSLog(@"%@",likevc.descript);
    
    NSLog(@"%@",likevc.photo_url);
    

    likevc.modalPresentationStyle = UIModalPresentationFullScreen;
    
   likevc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    
#pragma mark  死亡如风
    [self presentViewController:likevc animated:YES completion:^{
        
        
        
        
    }];
    
    [likevc release];
}

#pragma mark  KVO(键值对观察者)

-(void)creatKVOAction{
    [self.collect addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:@"ddd"];
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    
    CGFloat x = [[change objectForKey:@"new"]CGPointValue].x;
    CGFloat redX = x * WIDTH / 2 / WIDTH;
    
    self.viewOfred.transform = CGAffineTransformMakeTranslation(redX, 0);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
